﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wedo.Recognition.Domain.Models;
using Wedo.Recognition.Domain;
using Wedo.Recognition.Domain;
using Wedo.Service.Common;
using Wedo.Recognition.Service.Service;
using Wedo.Recognition.Repository;



namespace Wedo.Recognition.Service
{
    /// <summary>
    /// 申请争议处理
    /// </summary>
    public class DisputeApplyService : BaseService<DisputeApply>
    {
        public DisputeApplyService(IUnitWork dbContext)
            : base(dbContext, dbContext.Set<DisputeApply>())
        {

        }

        #region 对争议的处理
        /// <summary>
        /// 申请争议处理
        /// </summary>
        /// <param name="id">银行账单数据ID</param>
        /// <param name="applyMan">申请人</param>
        public void ApplyDispute(Guid id, string applyMan, string disputeResean)
        {

            BankDataService bankDataService = new BankDataService(_dbContext);

            var InBankData = _dbContext.Set<BankData>().Find(id);

            if (InBankData.IsDelete)
                throw new Exception("账单被废除，如果继续操作请恢复！");
            //已经申请争议处理，还没有审批通过
            if (InBankData.DisputeApply.Count(s => s.ApplyMan == applyMan && s.Status == 1) > 0)
                throw new Exception("你已经申请了争议处理，请等待审批通过");

            if (InBankData.CheckRecord.Count == 0)
                throw new Exception("没有签出，不需要争议！");
            if (InBankData.HasChecked(applyMan) && InBankData.Paymented() == 0)
            {
                throw new Exception("未认款，不需要争议！");
            }
            
            var disputeApplyItem = new DisputeApply()
            {
                BankDataId = InBankData.Id, 
                ApplyMan = applyMan,
                ApplyTime = DateTime.Now,
                Status=1 ,
                DisputeReason = disputeResean 
            };

            var model=  _repository.Create(disputeApplyItem); 
            Commit();

        }

        /// <summary>
        /// 取消争议申请
        /// </summary>
        /// <param name="id">争议请求编号</param>
        /// <param name="applyMan">取消申请人</param>
        public void CancelDisputeItem(Guid id, string applyMan)
        {
            BankDataService bankDataService = new BankDataService(_dbContext);
            DisputeApply dispApplyItem = GetById(id);

            if (dispApplyItem.Status > 1)
                throw new Exception("此争议已经被审批，不能再撤销");

            BankData bd = bankDataService.GetById(dispApplyItem.BankDataId);
            var myCheck = bd.CheckRecord.FirstOrDefault(m => m.ApplyMan == applyMan);
            if (myCheck != null)
            {
                if (bd.RefundmentApply.Any(m => m.Status == 2&&myCheck.Id==m.CheckRecordID))
                    throw new Exception("有退款,不能撤销");

                if (bd.PaymentApply.Any(m => m.Status == 2 && myCheck.Id == m.CheckRecordID))
                    throw new Exception(Wedo.Recognition.Resources.ErrorMessage.Partial_Confirm);
                //删除签出
                bankDataService.DeleteRecord(myCheck.Id);
            }
            //删除争议
            _repository.Delete(dispApplyItem);

            Commit();
        }

        /// <summary>
        /// 审批争议申请
        /// </summary>
        /// <param name="lid">争议申请编号</param>
        /// <param name="applyMan">争议申请人</param>
        public void ApproveDispute(Guid id, string applyMan)
        {
            DisputeApply dispApplyItem = GetById(id);

            if (dispApplyItem.Status > 1)
                throw new Exception("此争议已经审批通过，无需重复批准");

            dispApplyItem.Status = 2;
            dispApplyItem.Approver = applyMan;
            dispApplyItem.ApprovelTime = DateTime.Now;

            //如果没签出则创建申请人的签出
            var InBankData = _dbContext.Set<BankData>()
                .Find(dispApplyItem.BankDataId);
            if (InBankData.HasChecked(dispApplyItem.ApplyMan) == false)
            {
                CheckRecord record = new CheckRecord
                {
                    BankDataId = InBankData.Id,
                    BankData = InBankData,
                    Assiginment=InBankData.Assiginment,
                    ApplyTime = DateTime.Now,
                    ApplyMan = dispApplyItem.ApplyMan
                };
                _dbContext.Set<CheckRecord>()
                    .Create(record);
            }
            Commit();
        }

        /// <summary>
        /// 拒绝争议申请
        /// </summary>
        /// <param name="id">争议申请编号</param>
        /// <param name="approveMan">争议审批人</param>
        public void RejectDispute(Guid id, string approveMan)
        {
            DisputeApply dispApplyItem =GetById(id);
 
            if (dispApplyItem.Status > 1)
                throw new Exception(Wedo.Recognition.Resources.ErrorMessage.Has_Approve);

            dispApplyItem.Status = 3;
            dispApplyItem.Approver = approveMan;
            dispApplyItem.ApprovelTime = DateTime.Now;

            Commit();
        }
        #endregion

    }
}
